
#include "charQueue.h"
// 这里考虑到单片机应用的队列
void queueArrayInit(struct queueArrayInformStruct *finf, uint8_t *usrp, queuetype savelen)
{
    if(finf == NULL)
    {
        return;
    }
    memset(finf, 0, sizeof(struct queueArrayInformStruct));
    finf->saveBuf = usrp;
    finf->bufLen = savelen;
}

queuetype queueArrayInputString(struct queueArrayInformStruct *finf, const uint8_t *inbuf, queuetype len)
{
    queuetype i;
    if(!(finf != NULL && inbuf != NULL && finf->saveBuf != NULL))
    {
        return 0;
    }
    for(i = 0; i < len; i++)
    {
        finf->saveBuf[finf->windex] = inbuf[i];
        finf->windex = CYCLE_ADD(finf->windex, 1, finf->bufLen);
    }
    return len;
}

queuetype queueArraySize(struct queueArrayInformStruct *finf)
{
    if(finf == NULL)
    {
        return 0;
    }
    return (finf->windex + finf->bufLen - finf->rindex) % finf->bufLen;
}

queuetype queueArrayGetString(struct queueArrayInformStruct *finf, uint8_t *outbuf, queuetype len)
{
    queuetype i, tmplen;
    if(!(finf != NULL && outbuf != NULL && finf->saveBuf != NULL))
    {
        return 0;
    }
    tmplen = queueArraySize(finf);
    tmplen = tmplen > len ? len : tmplen;
    for(i = 0; i < tmplen; i++)
    {
        outbuf[i] = finf->saveBuf[finf->rindex];
        finf->rindex = CYCLE_ADD(finf->rindex, 1, finf->bufLen);
    }
    return tmplen;
}

queuetype queueArrayGetFixedSizeString(struct queueArrayInformStruct *finf, uint8_t *outbuf, queuetype len)
{
    queuetype i;
    if(!(finf != NULL && outbuf != NULL && finf->saveBuf != NULL))
    {
        return 0;
    }
    if(queueArraySize(finf) < len)
    {
        return 0;
    }
    for(i = 0; i < len; i++)
    {
        outbuf[i] = finf->saveBuf[finf->rindex];
        finf->rindex = CYCLE_ADD(finf->rindex, 1, finf->bufLen);
    }
    return len;
}
